package com.springapp.symm.service;

import com.springapp.utils.GlobalUtils;
import com.springapp.utils.FileTools;
import com.springapp.utils.ShellUtil;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @author Tandan
 * @Date 17-3-21
 * @see
 */

@Service
public class GithubManager {




    public static void main(String[] args){
        //new GithubManager().dowloadFiles();
        //new GithubManager().updateFile();
    }

    /**
     * git add 上传实例
     */
    public void updateFile() throws Exception{
        String[] str1 = new String[]{"git","add","."};
        String[] str2 = new String[]{"git","commit","-m","'GithubWebCommit'"};
        String[] str3 = new String[]{"git","push"};
        File executionPath = new File( GlobalUtils.getBasePath() );
       // Process p=  Runtime.getRuntime().exec("ipconfig",null, executionPath );
       // System.out.println( p.toString());
       /*String[] str1 = new String[]{"git","add","."};
        Runtime.getRuntime().exec(str1,null, executionPath );

        Runtime.getRuntime().exec(str2 ,null, executionPath );

        Runtime.getRuntime().exec(str3,null, executionPath );*/

        if(ShellUtil.execCommoned(str1,executionPath)){
            if(ShellUtil.execCommoned(str2,executionPath)){
                if(ShellUtil.execCommoned(str3,executionPath)){
                    System.out.println("git 修改，并提交成功");
                }
            }
        }


    }

    public void removeFile(String rPath) throws Exception{
        String[] str1 = new String[]{"git","rm", rPath , "-r" };
        String[] str2 = new String[]{"git","commit","-m","'GithubWebCommit'"};
        String[] str3 = new String[]{"git","push"};
        File executionPath = new File( GlobalUtils.getBasePath()  );
        if(ShellUtil.execCommoned(str1,executionPath)) {
            if (ShellUtil.execCommoned(str2, executionPath)) {
                if (ShellUtil.execCommoned(str3, executionPath)) {
                    System.out.println("git 修改，并提交成功");
                }
            }
        }


    }


    public void dowloadFiles() throws Exception{
        System.out.println("开始请求下载.");
        String[] str1 = new String[]{"git","clone", GlobalUtils.getGithubDowloadPath()};
        File executionPath = new File( GlobalUtils.getBasePath() );
        FileTools.judgeIsFileAndFolder(GlobalUtils.getBasePath());
        if(ShellUtil.execCommoned(str1,executionPath)){
            System.out.println("git 创建，获取本地成功");
        }

    }




}
